package org.jeecg.modules.business.third.strategy.emsdms;

import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.business.third.strategy.emsdms.consts.MdsSystemConst;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.dock.entity.mds.MdsBaseResponseVO;
import org.jeecg.modules.dock.entity.mds.request.EmptyMaterialRackRequestVO;
import org.jeecg.modules.dock.entity.mds.request.MaterialTransferAndWarehousingRequestVO;
import org.jeecg.modules.dock.enums.MdsInterfaceTypeEnum;
import org.jeecg.modules.dock.enums.MdsStatusEnum;
import org.jeecg.modules.dock.service.MdsDockingRepeaterService;
import org.jeecg.modules.flow.sys.constant.FlowConstant;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    //
 * 创建人员:    zhaojun
 * 创建时间:    2024-01-02
 */
@Service(value = "y103DmsMove")
@RequiredArgsConstructor
@Slf4j
public class Y103DmsMove implements ThirdOperateStrategy {
    private final IThirdCommonService thirdCommonService;
    private final MdsDockingRepeaterService mdsDockingRepeaterService;

    @Override
    public boolean operate(Map<String, Object> map) {
        AtomicBoolean result = new AtomicBoolean(true);
        Integer type = (Integer) map.get("type");
        try {
            // 默认可以不传 默认值为0 1：配送 2：退空料架 3：叫空料架
            if (ObjectUtils.compare(type, 0) == 0) {
                List<WmsMoveReceiptMaterial> records = (List<WmsMoveReceiptMaterial>) map.get("records");
                WmsMoveReceipt wmsMoveReceipt = (WmsMoveReceipt) map.get("moveReceipt");
                List<MaterialTransferAndWarehousingRequestVO.Detail> list = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(records)) {
                    records.stream().forEach(t -> {
                        MaterialTransferAndWarehousingRequestVO.Detail detail = new MaterialTransferAndWarehousingRequestVO.Detail();
                        detail.setBatchId(t.getBatchCode());
                        // 产品反馈非必填
                        detail.setArm("");
                        detail.setDiameter(Integer.valueOf(t.getOutsideDiameter()));
                        detail.setWeight(t.getTransferQuantity().intValueExact());
                        detail.setWidth(new BigDecimal(t.getWidth()));
                        list.add(detail);
                    });

                    MaterialTransferAndWarehousingRequestVO vo = new MaterialTransferAndWarehousingRequestVO();
                    vo.setTaskCode(wmsMoveReceipt.getTaskCode());
                    vo.setOperateTime(new Date());
                    vo.setTaskType(MdsInterfaceTypeEnum.TL_ALLOT_STOCK.getCode());
                    // 接口不需要
//                    vo.setTaskStatus("");
                    // 按照文档写死
                    vo.setSite(MdsSystemConst.WASTE_OUTLET);
                    vo.setOperateReason("");
                    LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                    if (ObjectUtils.isNotEmpty(sysUser)) {
                        vo.setOperatorName(sysUser.getRealname());
                    }
                    vo.setRemark("");
                    vo.setPrepareArea(wmsMoveReceipt.getMaterialArea());

                    vo.setDetail(list);

                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.materialTransferAndWarehousing(vo);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
            if (ObjectUtils.compare(type, 2) == 0) {
                List<WmsMoveReceiptMaterial> records = (List<WmsMoveReceiptMaterial>) map.get("records");
                WmsMoveReceipt wmsMoveReceipt = (WmsMoveReceipt) map.get("moveReceipt");
                List<MaterialTransferAndWarehousingRequestVO.Detail> list = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(records)) {

                    EmptyMaterialRackRequestVO vo = new EmptyMaterialRackRequestVO();
                    vo.setTaskCode(wmsMoveReceipt.getTaskCode());
                    vo.setOperateTime(new Date());
                    vo.setTaskType(MdsInterfaceTypeEnum.TL_ALLOT_STOCK.getCode());
                    vo.setTaskStatus(MdsStatusEnum.RETURN_PALLET_READY.getCode());
                    vo.setSite(MdsSystemConst.WASTE_OUTLET);
                    vo.setOperateReason("");
                    LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                    if (ObjectUtils.isNotEmpty(sysUser)) {
                        vo.setOperatorName(sysUser.getRealname());
                    }
                    vo.setRemark("");

                    // 入库区域返回的站点
                    vo.setCurrentSite(wmsMoveReceipt.getMaterialSite());
                    // 咨询产品：退空料架产线不必填
                    vo.setLineName("");

                    MdsBaseResponseVO mdsBaseResponseVO = mdsDockingRepeaterService.emptyMaterialRack(vo);
                    if (ObjectUtils.isNotEmpty(mdsBaseResponseVO)) {
                        if (FlowConstant.ERROR.equals(mdsBaseResponseVO.getTarget())) {
                            result.set(false);
                        }
                    } else {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            }
        } catch (Exception e) {
            log.error("Y103DmsMove:异常，{}", e.getMessage(), e);
            result.set(false);
        }
        return result.get();
    }
}
